package com.xhh.nphm.web.controller.system;

import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.utils.DateUtils;
import com.xhh.nphm.system.domain.TmSysNoticeLog;
import com.xhh.nphm.system.service.SafficheService;
import com.xhh.nphm.system.service.TmSysNoticeLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * 公告访问记录
 *
 * @author liutao
 */
@RequestMapping("/notice")
@Controller
public class TmSysNoticeLogController extends BaseController {
    @Autowired
    private TmSysNoticeLogService tmSysNoticeLogService;
    @Autowired
    private SafficheService safficheService;

    @PostMapping("/log")
    @ResponseBody
    public Object addLog(String afficheId){
        //先判断访问记录是否存在，存在不添加，不存在添加
        TmSysNoticeLog tmSysNoticeLog = tmSysNoticeLogService.selectByUserIdAndAffId(getSysUser().getUserId(), Integer.parseInt(afficheId));
        if(tmSysNoticeLog == null){
            //修改公告为已读
            if (safficheService.updateAfficheRead(afficheId)) {
                int i = tmSysNoticeLogService.addLog( new TmSysNoticeLog(getSysUser().getUserId(), Integer.parseInt(afficheId), DateUtils.getNowDate()));
                if (i==1){
                    System.out.println("添加成功");
                    return "添加成功";
                }
            }
        }else {
            System.out.println("该访问记录已存在");
            return "该访问记录已存在";
        }
        return null;
    }
}
